var Post = require("../../../class/admin/post.c");
var Module = require("../../../class/admin/module.c");
var Reply = require("../../../class/_include/reply.c");
var _Post = new Post();
var _Module = new Module();
var _Reply = new Reply();
module.exports = {
    prefix: "/admin/post/",
    post: {
        "create": {
            des: {
                name: "createPost",
                content: "创建话题",
                params: [
                    {
                        key: "title",
                        type: "string"
                    },
                    {
                        key: "detail",
                        type: "string|HtmlDocument"
                    }
                ]
            },
            method: function* () {
                var self = this;
                print("创建话题");
                try {
                    var data = yield parse(self.request);
                    data ? true : (self.body = printError("信息填写不完整"));
                    var _post = yield new Post(data, self.session.admin).save();
                    self.body = _post;
                    yield _Module.upgradepost(_post._module, true);
                } catch (e) {
                    self.body = printError(e.toString());
                };
            }
        }
    },
    get: {
        "listbypage": {
            des: {
                name: "getPostListByPageInfo",
                content: "根据分页信息获取话题列表",
                params: [
                    {
                        key: "page",
                        type: "Number"
                    },
                    {
                        key: "num",
                        type: "Number"
                    }
                ]
            },
            method: function* () {
                var self = this;
                print("根据分页信息获取话题列表");
                try {
                    data = self.request.query;
                    var _list = yield _Post.getListByPage(data);
                    self.body = _list;
                } catch (e) {
                    self.body = printError(e);
                };
            }
        },
        "listbymodule": {
            des: {
                name: "getPostListByModule",
                content: "根据版块信息获取话题列表",
                params: [
                    {
                        key: "page",
                        type: "Number"
                    },
                    {
                        key: "num",
                        type: "Number"
                    },
                    {
                        key: "_module",
                        type: "String"
                    }
                ]
            },
            method: function* () {
                var self = this;
                print("根据版块信息获取话题列表");
                try {
                    var data = self.request.query;
                    var _list = yield _Post.getListByModule(data);
                    self.body = _list;
                } catch (e) {
                    self.body = printError(e);
                };
            }
        },
        "detail/:_id": {
            des: {
                name: "getPostDetail",
                content: "获取话题详情",
                params: [
                    {
                        params: "_id",
                        type: "string"
                    }
                ]
            },
            method: function* () {
                var self = this;
                print("获取话题详情");
                try {
                    var _id = self.params._id;
                    var _detail = yield _Post.detail(_id);
                    self.body = _detail;
                } catch (e) {
                    self.body = printError(e.toString());
                };
            }
        },
        "count": {
            des: {
                name: "postCount",
                content: "话题统计",
                params: [
                    {
                        key: "creater_id",
                        type: "string"
                    },
                    {
                        key: "_module_id",
                        type: "string"
                    }
                ]
            },
            method: function* () {
                var self = this;
                print("获取话题统计");
                try {
                    var data = self.request.query;
                    var _count = yield _Post.count(data);
                    self.body = _count;
                } catch (e) {
                    self.body = printError(e);
                };
            }
        }
    },
    put: {
        "update": {
            des: {
                name: "postUpdate",
                content: "更新话题信息",
                params: [
                    {
                        key: "post",
                        type: "Post"
                    }
                ]
            },
            method: function* () {
                var self = this;
                print("更新话题信息")
                try {
                    var data = yield parse(self.request);
                    var _updated_post = yield _Post.update(data);
                    self.body = _updated_post;
                } catch (e) {
                    self.body = printError(e);
                };
            }
        }
    },
    delete: {
        "delete/:_id": {
            des: {
                name: "postDelete",
                content: "删除指定板块",
                params: [
                    {
                        params: "_id",
                        type: "string"
                    }
                ]
            },
            method: function* () {
                var self = this;
                try {
                    var _id = self.params._id;
                    var _post = yield _Post.delete(_id);
                    self.body = _post;
                    if (_post.type !== "error") {
                        yield _Module.upgradepost(_post._module);
                    };
                } catch (e) {
                    self.body = printError(e);
                };
            }
        }
    }
}
